package org.ccu.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccu.eshop.domain.dto.MerchandiseVo;
import org.ccu.eshop.service.MerchandiseServiceltface;
import org.ccu.eshop.service.impl.MerchandiseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/merchandise")
public class MerchandiseController extends HttpServlet {

    MerchandiseServiceltface merchandiseService=new MerchandiseServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("m");
        if ("getMerchantListBySpecial".equals(method)){
            getMerchantListBySpecial(request,response);
        }else if("getMerchantById".equals(method)){
            getMerchantById(request,response);
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


    /**
     *根据商品id来拿到商品的详细信息
     * @param request
     * @param response
     */
    private void getMerchantById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        String id=request.getParameter("id");
        MerchandiseVo merchandiseVo=merchandiseService.getMerchantById(id);
        //JSOn写回
        String  data=JSON.toJSONString(merchandiseVo);
        response.getWriter().write(data);

    }

    //根据是否特价 查询商品集合
    public void getMerchantListBySpecial(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String special=request.getParameter("special");
        String query=request.getParameter("query");
        List<MerchandiseVo> list=merchandiseService.getMerchantListBySpecial(special,query);
        String listJson= JSON.toJSONString(list);
        response.getWriter().write(listJson);
    }

}
